﻿var Normal_SearchAddList;
var Normal_SearchAddStore;
// 功能模块grid展示
Normal_SearchAddList = function (tagId) {
    var url = "/manager/Normal_TagMap/SearchList/";
    var sm = Ext.create('Ext.selection.CheckboxModel');
    var searchWord = "";
    var strPath = "";
    //grid 填充
    Ext.define('Normal_SearchAddGridModel', {
        extend: 'Ext.data.Model',
        fields: [
           { name: "Id", type: "string" },
            { name: "ChannelId", type: "string" },
            { name: "Title", type: "string" },
            { name: "AddDate", type: "datetime" },
            { name: "IsPass", type: "string" },
            { name: "IsImg", type: "string" },
            { name: "UserId", type: "string" },
            { name: "Enabled", type: "string" }
            ]
    });

    //grid获取 json数组
    Normal_SearchAddStore = Ext.create('Ext.data.Store', {
        pageSize: CMN.pageSize,
        model: 'Normal_SearchAddGridModel',
        buffered: false,  //是否动态加载
        proxy: {
            actionMethods: 'post',
            type: 'ajax',
            url: url,
            reader: {
                type: 'json',
                root: 'root',
                totalProperty: 'totalPorperty'
            },
            extraParams: {
                s: searchWord,
                path: strPath

            }
        },
        sorters: [{
            property: 'Id',
            direction: 'DESC'
        }]
    });

    Ext.define("TagsChannel", {
        extend: 'Ext.data.Model',
        proxy: {
            actionMethods: 'post',
            type: 'ajax',
            url: '/Manager/Module_Manager/List2/',
            reader: {
                type: 'json',
                root: 'root',
                totalProperty: 'totalPorperty'
            }
        },

        fields: ['Id', 'Title', 'Path']
    });

    //模板
    var channelStore = Ext.create('Ext.data.Store', {
        // autoLoad: true,
        pageSize: 1000,
        model: 'TagsChannel'
    });

    //分页设置
    var pagingBar = Ext.create('Ext.PagingToolbar', {
        displayInfo: true, emptyMsg: "没有数据显示", displayMsg: "显示从{0}条数据到{1}条数据，共{2}条数据",
        store: Normal_SearchAddStore,
        pageSize: CMN.pageSize,
        //显示右下角信息
        prevText: "上一页",
        nextText: "下一页",
        refreshText: "刷新",
        lastText: "最后页",
        firstText: "第一页",
        beforePageText: "当前页",
        afterPageText: "共{0}页"
    });

    var SearchList = function () {

        searchWord = Ext.getCmp("searchTxt").getValue();
        strPath = Ext.getCmp("ChannelPath").getValue();
        Normal_SearchAddStore.proxy.extraParams.s = searchWord;
        Normal_SearchAddStore.proxy.extraParams.path = strPath;
        Normal_SearchAddStore.loadPage(1);

    }
    //删除选中行信息
    SaveTagNormal_TagMapFn = function () {
        var row = Ext.getCmp("Normal_SearchAddPanel").getSelectionModel().getSelection();
        if (row.length == 0) {
            Ext.Msg.alert("提示信息", "请您至少选择一个!");
        }
        else if (row.length > 1) {
            var idList = "";
            for (var i = 0; i < row.length; i++) {
                if (i == 0) {
                    idList = row[0].data.Id;
                }
                else {
                    idList = idList + "_" + row[i].data.Id;
                }
            }
            //Ext.Msg.alert("提示信息", "对不起只能选择一个!");
            Ext.Msg.confirm("提 示!", "您确定要新增Tag" + idList + "吗?", function (btn) {
                if (btn == "yes") {
                    Ext.Ajax.request({
                        url: "/manager/Normal_TagMap/Insert/"+tagId+"?moduleIdList="+idList,
                        method: "get",
                        success: function (d) {
                            //成功后
                            var action = Ext.JSON.decode(d.responseText);
                            //alert(action.success);
                            var flag = action.success;
                            if (flag == "true") {
                                Normal_SearchAddStore.loadPage(pagingBar.getPageData().currentPage);
                             Ext.helper.msg("恭喜", "新增Tag成功!");
                            }
                            else {
                                var message = action.result.message;
                                if (message) {
                                    Ext.MessageBox.alert("新增Tag失败!", message);
                                }
                                else {
                                    Ext.MessageBox.alert("提示!", "新增Tag失败!");
                                }
                            }
                        },
                        failure: function (form, action) {
                            Ext.MessageBox.alert("提示!", "新增Tag失败!");
                        }
                    })
                }
                else {

                }
            })
        }
        else if (row.length == 1) {
            Ext.Msg.confirm("提 示!", "您确定要新增Tag" + row[0].data.Id + "吗?", function (btn) {
                if (btn == "yes") {
                    Ext.Ajax.request({
                        url: "/manager/Normal_TagMap/Insert/"+tagId+"?moduleIdList="+row[0].data.Id,
                        method: "POST",
                        success: function (d) {
                            //成功后
                            var action = Ext.JSON.decode(d.responseText);
                            //alert(action.success);
                            var flag = action.success;
                            if (flag == "true") {
                                Normal_SearchAddStore.loadPage(pagingBar.getPageData().currentPage);
                                Ext.helper.msg("恭喜", "新增Tag成功!");
                            }
                            else {
                                var message = action.result.message;
                                if (message) {
                                    Ext.MessageBox.alert("新增Tag失败!", message);
                                }
                                else {
                                    Ext.MessageBox.alert("提示!", "新增Tag失败!");
                                }
                            }
                        },
                        failure: function (form, action) {
                            Ext.MessageBox.alert("提示!", "新增Tag失败!");
                        }
                    })
                }
                else {

                }
            })
        }
    }


    //修改选中行信息
    EditNormal_SearchAddFn = function () {
        var row = Ext.getCmp("Normal_SearchAddPanel").getSelectionModel().getSelection();
        if (row.length == 0) {
            Ext.Msg.alert("提示信息", "您没有选中任何行!");
        }
        else if (row.length > 1) {
            Ext.Msg.alert("提示信息", "对不起只能选择一个!");
        } else if (row.length == 1) {
            EditLookArticleContent(row[0].data.Id);
        }

    }
    //grid配置
    var grid = Ext.create('Ext.grid.Panel', {
        id: "Normal_SearchAddPanel",
        layout: "fit",
        alias: 'widget.methodPanel',
        columnLines: true,
        loadMask: true,
        frame: true,
        border: false,
        selModel: sm,   //选择框
        autoScroll: true,
        store: Normal_SearchAddStore,
        ds: Normal_SearchAddStore,
        sm: sm,
        columnLines: true,
        //cm: cm2,
        viewConfig: {
            stripeRows: true,
            trackOver: true,
            enableTextSelection: true,
            emptyText: '<div style="color:#999;margin:5px;">当前没有记录显示<div>'
        },
        stripeRows: true,
        listeners: {
            "itemdblclick": function (g, rowIndex, e) {
                EditNormal_SearchAddFn();
            }
        },
        bbar: pagingBar,
        tbar: [{
            text: "选择新增Tag",
            tooltip: "选择新增Tag",
            iconCls: "addicon",
            handler: SaveTagNormal_TagMapFn
        }, {
            id: "ChannelPath",
            name: "ChannelPath",
            fieldLabel: "所属栏目",
            xtype: "combo",
            //传入后台真实值value field /value
            anchor: '90%',
            store: channelStore,
            autoRender: false,
            displayField: 'Title',
            queryMode: 'Path',
            valueField: 'Path'
        }, "", {
            xtype: 'textfield',
            name: 'searchTxt',
            id: 'searchTxt',
            emptyText: '输入搜索关键词'
        }, "", {
            text: "搜索",
            tooltip: "搜索",
            handler: SearchList
        }],
        columns: [
               Ext.create('Ext.grid.RowNumberer'),
                { header: "自增Id", dataIndex: "Id", width: 80, sortable: true },
                { header: "标题", dataIndex: "Title", width: 300, sortable: true },
                { header: "删除", dataIndex: "Id", renderer: function (value) { return "<a href='javascript:void(0);' title='删除' onclick='DeleteNormal_SearchAddFn()'class='deleteBtn' >&nbsp;&nbsp;&nbsp;</a>" }, width: 40 }
            ]
    });
    Normal_SearchAddStore.loadPage(1);


    ListNormal_SearchAddWin = Ext.create('Ext.window.Window', {
        title: "模板详细信息列表",
        width: 800,
        height: 450,
        plain: true,
        layout: "fit",
        //不可以随意改变大小
        resizable: false,
        //是否可以拖动
        //draggable:false,
        collapsible: true, //允许缩放条
        //closeAction: 'hide',
        closable: true,
        //弹出模态窗体
        modal: 'true',
        items: [grid]
    });
    ListNormal_SearchAddWin.show();

}
   
